// var requests = require('requests');
var fs = require('fs');
var path = require('path');
var path = require('zlib');

// requests('http://img1.money.126.net/data/hs/time/today/1399001.json')
// .on('data', function (chunk) {
//     fs.writeFile(path.resolve(__dirname,'data.json'),chunk,() =>{})
//   console.log(chunk)
// })
const request = require('request');
// request('http://img1.money.126.net/data/hs/time/today/1399001.json', function (error, response, body) {
//   console.error('error:', error); // Print the error if one occurred
//   console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
//   console.log('body:', body); // Print the HTML for the Google homepage.
// });
// 创建可读流
var writStream = fs.createWriteStream('1399001.gzip');
request('http://img1.money.126.net/data/hs/time/today/1399001.json').pipe(writStream)
// 处理流事件 --> data, end, and error
writStream.on('finish', function (chunk) {
  console.log("执行完毕")
  const compressing = require('compressing');
  // uncompress a file
  new compressing.gzip.UncompressStream({ source: '1399001.gzip' })
    //   .on('error', handleError)
    .pipe(fs.createWriteStream('1399001.txt'))
  /*.on('error', handleError)*/;
});
